package com.ibeeking.found.uaa.open.config.auth.open;

import com.ibeeking.found.uaa.open.config.auth.token.AbstractCustomTokenGranter;
import com.ibeeking.found.uaa.open.model.OpenUserDetails;
import com.ibeeking.found.uaa.open.service.IOpenUserDetailsService;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.OAuth2RequestFactory;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;

import java.util.Map;

/**
 * @Author ibeeking
 */
public class OpenTokenGranter extends AbstractCustomTokenGranter {

    private static final String GRANT_TYPE = "open_token";
    private static final String APP_ID = "app_id";
    private static final String APP_SECRET = "app_secret";

    private final IOpenUserDetailsService userDetailsServiceImpl;

    public OpenTokenGranter(AuthorizationServerTokenServices tokenServices, ClientDetailsService clientDetailsServiceImpl, OAuth2RequestFactory requestFactory, IOpenUserDetailsService userDetailsServiceImpl) {
        super(tokenServices, clientDetailsServiceImpl, requestFactory, GRANT_TYPE);
        this.userDetailsServiceImpl = userDetailsServiceImpl;
    }

    @Override
    protected OpenUserDetails getUserDetails(Map<String, String> parameters) {
        return userDetailsServiceImpl.openLogin(parameters.get(APP_ID), parameters.get(APP_SECRET));
    }
}
